package xml;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

import com.systech.core.util.xml.XMLUtils;
import com.systech.core.util.xml.xstream.XStreamSimple;
import com.thoughtworks.xstream.io.xml.DomWriter;

public class DocumentTest {

	public static void main(String[] args) {

		try {

			XStreamSimple xStream = new XStreamSimple();

			Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
			
			Map<String , Object> map = new HashMap<String, Object>();
			
			Person person = new Person();
			
			person.setName("oscar");
			person.setId(12);
			
			map.put("persona", person);

			//Convierte el objeto a un documento XML.
			xStream.marshal(map, new DomWriter(document));
			
			//Convierte el documento xml en un string
			String s = XMLUtils.documentToString(document);

			System.out.println("XML: "+s);

			//Para dar formato al xml
			String xml = xStream.toXML(map);
			System.out.println("XML con formato:");
			System.out.println(xml);
			
		}
		catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static String fncObtenerStringDoc(Document doc)throws Exception{ 
        TransformerFactory tFactory = TransformerFactory.newInstance(); 
        Transformer transformer = tFactory.newTransformer(); 
        DOMSource source = new DOMSource(doc); 
        StringWriter sw=new StringWriter(); 
        StreamResult result = new StreamResult(sw); 
        transformer.transform(source, result); 
        String xmlString=sw.toString(); 
        return xmlString; 
         
    }  
	
}
class Person {
	String name;
	Integer id;
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public Integer getId() {
		return id;
	}
	
	public void setId(Integer id) {
		this.id = id;
	}
	
}
